package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.dto.SearchRecordContentDTO;
import com.yuantu.judiciary.model.enums.AccountState;
import com.yuantu.judiciary.model.enums.SearchType;
import com.yuantu.judiciary.model.form.AddSearchRecordForm;
import com.yuantu.judiciary.service.ISearchRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索记录模块
 *
 * @author syw
 * @since 2023-07-07
 **/
@RestController
@RequestMapping(value = "/search-record")
@Api(tags = "搜索记录模块")
@RequiredArgsConstructor
public class SearchRecordController extends BaseController {

    private final ISearchRecordService searchRecordService;

    @ApiOperation(value = "添加搜索记录")
    @PostMapping("/add")
    public Response<Boolean> add(@RequestBody AddSearchRecordForm form) {
        Long userId = getLoginAccount().getAccountId();
        String res = searchRecordService.verify(userId,form);
        if(res.equals(AccountState.AUTH_NOT_PASS.name())){
            return ResponseFactory.errResponse(
                    "未进行企业认证，请通过【推广端】认证。");
        }
        return ResponseFactory.okResponse(searchRecordService.add(userId, form));
    }

    @ApiOperation(value = "获取搜索记录（最近搜索的10个）")
    @GetMapping("/list/limit")
    public Response<List<SearchRecordContentDTO>> listLimit(@RequestParam SearchType type) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(searchRecordService.listLimit(userId, type));
    }

    @ApiOperation(value = "删除所有搜索记录")
    @PostMapping("/delete/all")
    public Response<Boolean> deleteAll(@RequestParam SearchType type) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(searchRecordService.deleteAll(userId, type));
    }

    @ApiOperation(value = "获取热搜记录")
    @GetMapping("/list/hotSearch")
    public Response<List<SearchRecordContentDTO>> hotSearch() {
        return ResponseFactory.okResponse(searchRecordService.hotSearch());
    }
}
